Trie树(模糊匹配)poj1816

Language:
Wild Words
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4681 Accepted: 1221

Description

A word is a string of lowercases. A word pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases.  

There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it.  

Input

The first line of input contains two integers N (0 < N <= 100000) and M (0 < M <=100), representing the number of word patterns and the number of words. Each of the following N lines contains a word pattern, assuming all the patterns are numbered from 0 to N-1. After those, each of the last M lines contains a word.  

You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20.  

Output

For each word, print a line contains the numbers of matched patterns by increasing order. Each number is followed by a single blank. If there is no pattern that can match the word, print "Not match".

Sample Input

5 4
t*
?h*s
??e*
*s
?*e
this
the
an
is

Sample Output

0 1 3 
0 2 4 
Not match
3
思路:对模式串建树,然后dfs找匹配,具体见代码注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000010;
int N,M;
char s[50];
vector<int> ans;

struct node
{
    node *next[30];
    vector<int> val;
    node(){memset(next,0,sizeof(next));}
};
struct TREE
{
    node *root;
    int len;
    void clear(){root=new node();}
    int idx(char x)
    {
        if(x=='?')return 26;
        if(x=='*')return 27;
        return x-'a';
    }
    void insert(char *s,int id)
    {
        int n=strlen(s);
        node *p=root;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!p->next[c])
                p->next[c]=new node();
            p=p->next[c];
        }
        p->val.push_back(id);
    }
    void dfs(char *s,node *p,int pos)
    {
        if(s[pos]!=0)//当前字符不是结束符时,有三种情况可以匹配,分别是字母,?和*
        {
            int c=idx(s[pos]);
            if(p->next[c])dfs(s,p->next[c],pos+1);
            if(p->next[26])dfs(s,p->next[26],pos+1);
            if(p->next[27])//如果是*因为它可以匹配多个,所以从可以从当前位置到最后随便选一个开始匹配
                for(int i=pos;i<=len;i++)//这里要到等于len,因为这个*可以直接匹配所有省下的字符
                    dfs(s,p->next[27],i);
        }
        else
        {
            for(int i=0;i<p->val.size();i++)ans.push_back(p->val[i]);
            if(p->next[27])dfs(s,p->next[27],pos);//因为*可以不匹配,所以如果s传结束了,但是模式串可能还没结束
        }
    }
    void find(char *s)
    {
        len=strlen(s);
        ans.clear();
        dfs(s,root,0);
        sort(ans.begin(),ans.end());
        ans.resize(distance(ans.begin(),unique(ans.begin(),ans.end())));
        int n=ans.size();
        if(!n)printf("Not match\n");
        else
        {
            for(int i=0;i<n-1;i++)printf("%d ",ans[i]);
            printf("%d\n",ans[n-1]);
        }
    }
}tree;
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        tree.clear();
        for(int i=0;i<N;i++)
        {
            scanf("%s",s);
            tree.insert(s,i);
        }
        while(M--)
        {
            scanf("%s",s);
            tree.find(s);
        }
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值